package com.guanzi.big_data_mq_process.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author 管子
 * @date 2025/6/13 22:34
 * @description: ThreadPoolConfig 类
 */
@Configuration
public class ThreadPoolConfig {

    @Bean("exportThreadPool")
    public ThreadPoolExecutor exportThreadPool() {
        return new ThreadPoolExecutor(
                5,
                20,
                60L, TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(10),
                new CustomThreadFactory("导出线程", 5));
    }

    @Bean("importThreadPool")
    public ThreadPoolExecutor importThreadPool() {
        return new ThreadPoolExecutor(
                5,
                20,
                60L, TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(10),
                new CustomThreadFactory("导入线程", 5));
    }

    @Bean("importParsingThreadPool")
    public ThreadPoolExecutor importParsingThreadPool() {
        return new ThreadPoolExecutor(
                20,
                20,
                60L, TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(10),
                new CustomThreadFactory("导入解析线程", 20));
    }



    static class CustomThreadFactory implements ThreadFactory {

        private String name;
        private int index;

        public CustomThreadFactory(String name, int index) {
            this.name = name;
            this.index = index;
        }

        @Override
        public Thread newThread(Runnable task) {
            Thread thread = new Thread(task);
            thread.setName(name + "-" + index++);
            return thread;
        }

    }
}
